package ext

import manager.AppTask

class AppTimer (private val cb: () -> Unit ) : Runnable {

    private var runFlag = false

    fun cancel() {
        if (!runFlag) {
            return
        }
        runFlag = false
        AppTask.removeCallbacks(this)
    }

    fun start(ms: Long) {
        if (runFlag) {
            cancel()
        }
        runFlag = true
        AppTask.postDelayed(this, ms)
    }

    override fun run() {
        if (!runFlag) {
            return
        }
        cb()
        runFlag = false
    }
}